<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      padding: 0;
      margin: 0;
      box-sizing: border-box;
    }

    .cls {
      width: 400px;
      height: 200px;
      background-color: hotpink;
    }
  </style>
</head>

<body>
  <div class="cls">

  </div>
</body>
<script>
  "use strict";
  let str = `
    * {
      padding: 0;
      margin: 0;
      box-sizing: border-box;
    }

    .cls {
      width: 400px;
      height: 200px;
      background-color: hotpink;
    }
  `;
  function compressCss(s) {
    //压缩代码
    s = s.replace(/\/\*(.|\n)*?\*\//g, ""); //删除注释
    s = s.replace(/\s*([\{\}\:\;\,])\s*/g, "$1");
    s = s.replace(/\,[\s\.\#\d]*\{/g, "{"); //容错处理
    s = s.replace(/;\s*;/g, ";"); //清除连续分号
    s = s.match(/^\s*(\S+(\s+\S+)*)\s*$/); //去掉首尾空白
    return s == null ? "" : s[1];
  }
  let i = compressCss(str);
  console.log(i);
  
</script>

</html>